//
//  KLineRightView.swift
//  JunYiProject
//
//  Created by xiaogxjkz on 2021/6/11.
//

import UIKit

class KLineRightView: BaseView {

    ///副图当前指标索引
    lazy var minorCurrentIndex:Int = 0
    
    private weak var viewModel:GameViewModel?
    private let cell_identify:String = "KLineRightViewIdentify"
    
    
    //MARK: - override
    override init(ViewModel vm: ViewModelProtocol) {
        self.viewModel = vm as? GameViewModel
        super.init(ViewModel: vm)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 设置约束
    override func updateConstraints() {
        self.listTableView.snp.makeConstraints {[weak self] make in
            guard let self = self else { return }
            make.edges.equalTo(self)
        }
        
        super.updateConstraints()
    }
    
    
    /// 添加视图
    override func setupViews() {
        self.addSubview(self.listTableView)

        setNeedsUpdateConstraints()
    }
    
    
    /// 绑定事件
    override func bindViewModel() {
        
    }
    
    
    //MARK: - lazy load
    private lazy var listTableView:UITableView = {[weak self] in
        autoreleasepool {
            autoreleasepool {
                let _tb = UITableView.init(frame: .zero,style: .plain)
                _tb.backgroundColor = UIColor.clear
                
                _tb.showsVerticalScrollIndicator = true
                _tb.showsHorizontalScrollIndicator = false
                
                //分割线
                _tb.separatorStyle = .none
                _tb.separatorInset = .init(top: 0, left: 0, bottom: 0, right: 0)
                _tb.separatorColor = Setting.K_APP_SPLIT_LINE_COLOR
                
                //行选中
                _tb.allowsSelection = true
                _tb.allowsMultipleSelection = false
                
                //表头、表尾
                _tb.tableHeaderView = UIView.init(frame: .zero)
                _tb.tableFooterView = UIView.init(frame: .zero)
                
                _tb.delegate = self
                _tb.dataSource = self
                
                //防止顶部空白
                if #available(iOS 11.0, *) {
                    _tb.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
                }
                else {
                    //self.automaticallyAdjustsScrollViewInsets = false
                }
                
                //注册
                _tb.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: cell_identify)
                
                return _tb
            }
        }
    }()
}


//MARK: -
extension KLineRightView {
    
    /// 刷新
    func reloadSet(){
        self.listTableView.reloadData()
    }
}


//MARK: - UITableViewDelegate,UITableViewDataSource
extension KLineRightView : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return MarketSetting.arrMinorKPI.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        autoreleasepool {
            
            var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cell_identify, for: indexPath)
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: cell_identify)
            }
            cell?.backgroundColor = UIColor.clear
        
            var tag = 123
            var imgBg:UIImageView? = cell?.contentView.viewWithTag(tag) as? UIImageView
            if imgBg == nil {
                imgBg = UIImageView.init(image: YYImage.init(named: "game_select_bg.png"))
                
                imgBg?.tag = tag
                cell?.contentView.addSubview(imgBg!)
                imgBg?.snp.makeConstraints { make in
                    make.edges.equalTo(cell!.contentView)
                }
            }
             
            tag = 124
            var labTitle:YYLabel? = cell?.contentView.viewWithTag(tag) as? YYLabel
            if labTitle == nil {
                labTitle = BaseView.createLable(rect: .zero,
                                                text: nil,
                                                textColor: UIColor.white,
                                                font: UIFont.systemFont(ofSize: 16, weight: .medium))
                labTitle?.textAlignment = .center
                
                cell?.contentView.addSubview(labTitle!)
                labTitle?.snp.makeConstraints({ make in
                    make.edges.equalTo(cell!.contentView)
                })
                labTitle?.tag = tag
            }

            imgBg?.isHidden = !(self.minorCurrentIndex == indexPath.row)
            
            if MarketSetting.arrMinorKPI.count > indexPath.row {
                labTitle?.text = MarketSetting.arrMinorKPI[indexPath.row]
            }
            
            return cell!
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        defer {
            tableView.deselectRow(at: indexPath, animated: true)
            tableView.reloadData()
        }
        
        autoreleasepool {
            if MarketSetting.arrMinorKPI.count > indexPath.row {
                self.minorCurrentIndex = indexPath.row
                self.viewModel?.gameMarketKpiChangeCommand.execute((indexPath.row,MarketSetting.arrMinorKPI[indexPath.row]))
            }
        }
    }
}

